package com.ruoyi.produce.report.procedureProgress.service;


import com.ruoyi.produce.report.procedureProgress.domain.ProcedureProgress;
import com.ruoyi.produce.report.procedureProgress.mapper.ProcedureProgressMapper;
import com.ruoyi.produce.process.worksheet.domain.ProduceProcedureTransferSheet;
import com.ruoyi.produce.process.worksheet.mapper.ProduceWorkSheetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.List;

@Service
public class ProcedureProgressServiceImpl implements IProcedureProgressService {

    @Autowired
    private ProcedureProgressMapper procedureProgressMapper;

    @Autowired
    private ProduceWorkSheetMapper produceWorkSheetMapper;

    @Override
    public List<ProcedureProgress> selectProcedureProgressForm(ProcedureProgress procedureProgress) {
        return setOtherLists(procedureProgressMapper.selectProcedureProgressForm(procedureProgress));
    }

    public List<ProcedureProgress> setOtherLists(List<ProcedureProgress> procedureProgresses) {
        for (int i = 0; i < procedureProgresses.size(); i++) {
            //存入工序移交记录
            List<ProduceProcedureTransferSheet> produceProcedureTransferSheets = produceWorkSheetMapper.selectProduceProcedureTransferSheetList(new ProduceProcedureTransferSheet(procedureProgresses.get(i)));
            BigDecimal bigDecimalCN = new BigDecimal("0");//完成数量 completedNumber
            BigDecimal bigDecimalRN = new BigDecimal("0");//接收数量 receivedNumber
            BigDecimal bigDecimalSN = new BigDecimal("0");//报废数量 scrapNumber
            for (int j = 0, produceProcedureTransferSheetsSize = produceProcedureTransferSheets.size(); j < produceProcedureTransferSheetsSize; j++) {
                ProduceProcedureTransferSheet produceProcedureTransferSheet = produceProcedureTransferSheets.get(j);
                switch (produceProcedureTransferSheet.getTransferSheetStatus()) {
                    case "1"://完工
                        if (procedureProgresses.get(i).getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序时，增加完成数量
                            bigDecimalCN = bigDecimalCN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        if (procedureProgresses.get(i).getProcedureId().equals(produceProcedureTransferSheet.getProcedureTransferId())) {
                            //等于转到工序时，增加接收数量
                            bigDecimalRN = bigDecimalRN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                    case "2"://返工
                        if (procedureProgresses.get(i).getProcedureId().equals(produceProcedureTransferSheet.getProcedureTransferId())) {
                            //等于转到工序时，减少完成数量
                            bigDecimalCN = bigDecimalCN.subtract(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        if (procedureProgresses.get(i).getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序时，减少接收数量
                            bigDecimalRN = bigDecimalRN.subtract(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                    case "3":
                        //报废，无转到工序
                        //增加报废数量
                        if (procedureProgresses.get(i).getProcedureId().equals(produceProcedureTransferSheet.getProcedureCurrentId())) {
                            //等于当前工序
                            bigDecimalSN = bigDecimalSN.add(new BigDecimal(produceProcedureTransferSheet.getTransferNumber()));
                        }
                        break;
                }
            }
            procedureProgresses.get(i).setCompletedNumber(bigDecimalCN.doubleValue());
            procedureProgresses.get(i).setReceivedNumber(bigDecimalRN.doubleValue());
            procedureProgresses.get(i).setScrapNumber(bigDecimalSN.doubleValue());
            procedureProgresses.get(i).setUnfinishedNumber(procedureProgresses.get(i).getWorkSheetNumber() - bigDecimalCN.doubleValue());
        }
        return procedureProgresses;
    }

}
